package dir.simulation;

import java.util.HashMap;

public class Directory {
	protected HashMap<Character, Integer> char2Id;
	public int serverNum;
	public int prefixUsed;

	public Directory(int serverNum) {
		this.serverNum = serverNum;
		this.char2Id = new HashMap<Character, Integer>();
		this.prefixUsed = 3;
		initCharset();
	}

	public void initCharset() {
		String charset = "";
		charset += " _";
		for (int i = 0; i < 26; ++i) {
			charset += (char) ('a' + i);
			charset += (char) ('A' + i);
		}
		for (int i = 0; i < charset.length(); ++i) {
			char2Id.put(charset.charAt(i), i);
		}
	}

	public int lookup(String fileName) {
		int value = 0;
		int i;
		for (i = 0; i < Math.min(fileName.length(), prefixUsed); ++i) {
			value *= char2Id.size();
			value += char2Id.get(fileName.charAt(i));
		}
		while (i < prefixUsed) {
			value *= char2Id.size();
			i++;
		}
		int nodeId = value
				/ (int) Math.ceil(Math.pow(char2Id.size(), prefixUsed)
						/ serverNum) + 1;
		return nodeId;
	}
}
